import type { FieldConfig } from '@/components/TableFilter.vue'
import { getFormFields } from '@/utils/formUtils'
import type { FormFieldConfig } from '@/types/form'
import { title } from 'process'
import { usePromotionCodeStore } from '@/store/promotionCodeStore'
import { useReductionPlanStore } from '@/store/reductionPlanStore'

// 表格列配置
export const tableColumns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '名稱',
    dataIndex: 'name',
  },
  {
    title: '折扣卡集合',
    dataIndex: 'promotionCardSet',
  },
  {
    title: '減免方案',
    dataIndex: 'reductionPlan',
  },
  {
    title: '優先級',
    dataIndex: 'priority',
  },
  {
    title: '修改人',
    dataIndex: 'updatedBy',
  },
  {
    title: '修改時間',
    dataIndex: 'updatedAt',
  },
  {
    title: '操作',
    key: 'action',
    align: 'center',
  },
]
// 過濾項配置
export const filterFields: FieldConfig[] = [
  {
    name: 'name',
    label: '名稱',
    type: 'input',
    placeholder: '請輸入名稱',
  },
]

// 表單字段配置
const FormField: Record<string, FormFieldConfig> = {
  name: {
    label: '名稱',
    type: 'input',
    placeholder: '請輸入名稱',
    rules: [{ required: true, message: '請輸入名稱' }],
  },
  promotionCardSet: {
    label: '折扣卡集合',
    type: 'select',
    placeholder: '請輸入折扣卡集合',
    mode: 'multiple',
    options: [],
    tooltip: '或者滿足任意一折扣卡',
  },
  reductionPlanId: {
    label: '減免方案',
    type: 'select',
    placeholder: '請輸入減免方案',
    options: [],
    tooltip: '或者滿足減免方案',
  },
  priority: {
    label: '優先級',
    type: 'number',
    defaultValue: 1,
    min: 1,
    placeholder: '請輸入優先順序',
    rules: [{ required: true, message: '請輸入優先順序' }],
    tooltip: '數字越大,執行優先級越高',
  },
}

export const getCreteFieldConfig = async (fields?: string[]) => {
  const cardSetOptions = await fetchCardSetOptions()
  FormField.promotionCardSet.options = cardSetOptions
  const reductionPlanOptions = await fetchReductionPlanOptions('create')
  FormField.reductionPlanId.options = reductionPlanOptions
  return getFormFields(FormField, fields)
}

export const getEditFieldConfig = async (fields?: string[]) => {
  const cardSetOptions = await fetchCardSetOptions()
  FormField.promotionCardSet.options = cardSetOptions
  const reductionPlanOptions = await fetchReductionPlanOptions('edit')
  FormField.reductionPlanId.options = reductionPlanOptions
  return getFormFields(FormField, fields)
}

export const fetchCardSetOptions = async () => {
  const promotionCodeStore = usePromotionCodeStore()
  const type = 3
  const options = await promotionCodeStore.fetchPromotionCodeOptions(type)
  return options
}

const fetchReductionPlanOptions = async (type: string) => {
  const reductionPlanStore = useReductionPlanStore()
  const options = await reductionPlanStore.fetchOptions()
  return options
}
